home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 17 / AMIGAplus Sonderheft 17 (1999)(ICP)(DE)[!].iso / dsound / source.lha / PlayMono2.c < prev    next >
C/C++ Source or Header  |  1994-07-18  |  5KB  |  204 lines

  1.  
  2. /*************************************************************************/
  3. /*                  PlayMono2.c                 */
  4. /*     Contains code used to play mono samples out of both speakers.     */
  5. /*************************************************************************/
  6.  
  7. #include <exec/types.h>
  8. #include <exec/exec.h>
  9. #include <devices/audio.h>
  10. #include <dos/dos.h>
  11. #include <intuition/intuition.h>
  12. #include <intuition/intuitionbase.h>
  13. #include <graphics/gfxbase.h>
  14. #include <stdlib.h>
  15.  
  16. #include "dsound.h"
  17. #include "minrexx.h"
  18.  
  19. #include <proto/intuition.h>
  20. #include <proto/exec.h>
  21. #include <proto/dos.h>
  22.  
  23. extern UBYTE rightAMap[];
  24. extern UBYTE leftAMap[];
  25. extern UBYTE eitherAMap[];
  26. extern UBYTE bothAMap[];
  27.  
  28. extern UBYTE volume;
  29. extern UWORD speed;
  30. extern ULONG bufSize;
  31.  
  32. extern BOOL readAll;
  33. extern BOOL loop;
  34. extern struct Window *window;
  35.  
  36. extern ULONG signalMask;
  37.  
  38. extern BOOL rexxAbort;
  39.  
  40. /*Play a mono sample (or a single channel of a stereo sample) out of */
  41. /*both speakers simultaneously*/
  42. void playMonoTwice(BPTR file,channel audioChannel,struct Voice8Header *vhdr,
  43.            ULONG length)
  44. {
  45.    struct IOAudio *iob1_right,*iob2_right,*iob1_left,*iob2_left;
  46.    struct IOAudio *cur_right,*cur_left,*alt_right,*alt_left;
  47.    extern long arexxSigBit;
  48.    ULONG toRead;
  49.    ULONG sampleSize=length;
  50.    BOOL done=FALSE;
  51.    ULONG amountLeft;
  52.  
  53.    /*Read the entire sample into memory, if the user so specified*/
  54.    if(readAll)
  55.    {
  56.       storeLeft(file,length,bufSize);
  57.       file=0L;
  58.    }
  59.  
  60.    /*Get the first audio channel*/
  61.    iob1_left=GetAudioChannel(bufSize,leftAMap);
  62.    if(iob1_left==NULL)
  63.    {
  64.       WriteMsg("Couldn't create the first stereo buffer\n");
  65.       cleanup(150);
  66.    }
  67.  
  68.    iob1_right=GetAudioChannel(bufSize,rightAMap);
  69.    if(iob1_right==NULL)
  70.    {
  71.       WriteMsg("Couldn't create the second stereo buffer\n");
  72.       cleanup(150);
  73.    }
  74.    FreeMem(iob1_right->ioa_Data,iob1_right->ioa_Length);
  75.    iob1_right->ioa_Data=iob1_left->ioa_Data;
  76.  
  77.    /* If the user didn't specify a volume, get it from the VHDR */
  78.    if(volume==0)
  79.       volume=(vhdr->volume>>10);
  80.  
  81.    /* If the VHDR gave a volume of zero, use maximum volume*/
  82.    if(volume==0)
  83.       volume=64;
  84.  
  85.    /* Get the samples/sec rate (either the rate given by the user, or the*/
  86.    /* rate found in the VHDR) */
  87.    if(speed==0)
  88.       speed=1000000000/(vhdr->samplesPerSec*279);
  89.    else
  90.       speed=1000000000/(speed*279);
  91.  
  92.    InitAudioChannel(iob1_left,volume,speed);
  93.    InitAudioChannel(iob1_right,volume,speed);
  94.  
  95.    /*Get the 2nd audio channel*/
  96.    iob2_left=DuplicateAudioChannel(iob1_left);
  97.  
  98.    if(iob2_left==NULL)
  99.    {
  100.       FreeAudioChannel(iob1_left);
  101.       FreeAudioChannel(iob1_right);
  102.       WriteMsg("Couldn't create the second buffer");
  103.       cleanup(175);
  104.    }
  105.  
  106.    iob2_right=DuplicateAudioChannel(iob1_right);
  107.    if(iob2_right==NULL)
  108.    {
  109.       FreeAudioChannel(iob1_left);
  110.       DeleteDuplication(iob2_left);
  111.       FreeAudioChannel(iob1_right);
  112.       WriteMsg("Couldn't create the second buffer");
  113.       cleanup(175);
  114.    }
  115.    FreeMem(iob2_right->ioa_Data,iob2_right->ioa_Length);
  116.    iob2_right->ioa_Data=iob2_left->ioa_Data;
  117.  
  118.    /* Load the first buffer*/
  119.    toRead=MIN(length,bufSize);
  120.    LoadAudioBuffer(file,iob1_left,toRead);
  121.    iob1_right->ioa_Length=iob1_left->ioa_Length=toRead;
  122.    length-=toRead;
  123.  
  124.    if(length==0 && loop)
  125.    {
  126.       length=sampleSize;
  127.       if(!readAll)
  128.      Seek(file,-sampleSize,OFFSET_CURRENT);
  129.    }
  130.  
  131.    /*Initialize the sample position info*/
  132.    updateSampleInfo(0,sampleSize,vhdr->samplesPerSec);
  133.  
  134.    /*Queue up the play requests*/
  135.    BeginIO((struct IORequest *)iob1_left);
  136.    BeginIO((struct IORequest *)iob1_right);
  137.  
  138.    cur_right=iob2_right;
  139.    cur_left=iob2_left;
  140.    alt_right=iob1_right;
  141.    alt_left=iob1_left;
  142.  
  143.    /*Loop while there's stuff to read*/
  144.    while(!done && !rexxAbort)
  145.    {
  146.       toRead=MIN(length,bufSize);
  147.  
  148.       if(toRead!=0)
  149.       {
  150.      LoadAudioBuffer(file,cur_left,toRead);
  151.      cur_right->ioa_Length=cur_left->ioa_Length=toRead;
  152.      BeginIO((struct IORequest *)cur_left);
  153.      BeginIO((struct IORequest *)cur_right);
  154.      amountLeft=length-=toRead;
  155.  
  156.      if(length==0 && loop)
  157.      {
  158.         length=sampleSize;
  159.         if(!readAll)
  160.            Seek(file,-sampleSize,OFFSET_CURRENT);
  161.      }
  162.      done=FALSE;
  163.       }
  164.       else
  165.      done=TRUE;
  166.  
  167.       /*Wait for the second buffer to finish*/
  168.       if((Wait((1<<alt_right->ioa_Request.io_Message.mn_ReplyPort->mp_SigBit) |
  169.        signalMask | arexxSigBit) & SIGBREAKF_CTRL_C) == SIGBREAKF_CTRL_C)
  170.      done=TRUE;
  171.  
  172.       dispRexxPort();
  173.  
  174.       /*Update the sample position info*/
  175.       updateSampleInfo(sampleSize-amountLeft-toRead,sampleSize,vhdr->samplesPerSec);
  176.  
  177.       /*If we got a message from the window, it is a CLOSEWINDOW message*/
  178.       /*and we're done*/
  179.       if(window!=NULL && GetMsg(window->UserPort)!=NULL)
  180.       {
  181.      done=TRUE;
  182.       }
  183.       swapPointers(&cur_left,&alt_left);
  184.       swapPointers(&cur_right,&alt_right);
  185.    }
  186.  
  187.    /*Restore the buffer lengths, so that FreeAudio() channel, etc., knows*/
  188.    /*how much memory to free*/
  189.    iob1_left->ioa_Length=iob2_left->ioa_Length=bufSize;
  190.    iob1_right->ioa_Length=iob2_right->ioa_Length=bufSize;
  191.  
  192.    iob1_right->ioa_Data=NULL;
  193.    iob2_right->ioa_Data=NULL;
  194.  
  195.    FreeAudioChannel(iob1_left);
  196.    DeleteDuplication(iob2_left);
  197.    FreeAudioChannel(iob1_right);
  198.    DeleteDuplication(iob2_right);
  199.  
  200.    return;
  201. }
  202.  
  203. /*End of Play.c*/
  204.